package com.tgpms.web.assets.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.tgpms.exception.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.util.Common;
import com.tgpms.util.PageInfo;
import com.tgpms.web.assets.entity.FaRepair;
import com.tgpms.web.assets.service.FaRepairService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

/**
 * @author newway
 * <p>
 * D8002固定资产大修改造接口
 */
@Api(value = "FaRepairController", tags = "固定资产大修改造接口")
@RestController
@RequestMapping(value = "/ev/faRepair")
public class FaRepairController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private FaRepairService faRepairService;
	@Autowired
	private TokenService tokenService;
	
	
	/**
	* @Author newway
	* @Description 查询固定资产大修接口信息
	* @Date 2019年8月31日下午3:20:47
	* @Param 
	* @return 
	**/
	@ApiOperation(value = "查询固定资产大修接口信息", notes = "查询固定资产大修接口信息", httpMethod = "POST")
	@PostMapping(value = "/selFaRepairList")
	public Result selFaRepairList(@RequestParam(value = "selparam", required = false) String selparam,
			@RequestParam(value = "leftdition", required = false) String leftdition,
			@RequestParam(value = "condition", required = false) String condition,
	        @RequestParam(value = "dataowner", required = false) String dataowner,
		    @RequestParam(value = "pageNo", required = true) String pageNo,
			@RequestParam(value = "pageSize", required = true) String pageSize){
		Result result = new Result();
		PageInfo pageInfo = new PageInfo();
		if(Common.isEmpty(pageNo) || Common.isEmpty(pageSize)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			pageInfo.setPage(pageNo);
			pageInfo.setSize(pageSize);
			Map<String,String> map = new HashMap<String,String>();
			map.put("selparam", selparam);
			map.put("leftdition", leftdition);
			map.put("condition", condition);
			map.put("dataowner", dataowner);
			pageInfo.setQueryMap(map);
			result = faRepairService.selFaRepairList(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
			throw new QueryException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
		}
		return result;
	}
	
	
	
	/**
	* @Author newway
	* @Description 添加固定资产大修改造信息接口
	* @Date 2019年8月12日下午4:22:08
	* @Param com.tgpms.web.assets.entity.FaRepair
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "添加固定资产大修改造信息接口", notes = "添加固定资产大修改造信息接口", httpMethod = "POST")
	@PostMapping(value = "/addFaRepairInfo")
	public Result addFaRepairInfo(@RequestBody FaRepair FaRepair,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(FaRepair.getFixedAssetNo())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			//获取token操作人
			FaRepair.setInsertUser(tokenService.getUserName(request));
			FaRepair.setInsertDate(new Date());
			Map<String, String> map = faRepairService.addFaRepairInfo(FaRepair);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
				result.setData(map.get("data"));
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}
	

	

	/**
	* @Author newway
	* @Description 更新固定资产大修改造信息接口
	* @Date 2019年8月12日下午5:37:08
	* @Param com.tgpms.web.assets.entity.FaRepair
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "更新固定资产大修改造信息接口", notes = "更新固定资产大修改造信息接口", httpMethod = "PUT")
	@PutMapping(value = "/updateFaRepairInfo")
	public Result updateFaRepairInfo(@RequestBody FaRepair FaRepair,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(FaRepair.getFixedAssetNo()) || Common.isEmpty(FaRepair.getFaRepairNo())
				|| Common.isEmpty(FaRepair.getWorkNo())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			//获取token操作人
			FaRepair.setModifyUser(tokenService.getUserName(request));
			FaRepair.setModifyDate(new Date());
			Map<String, String> map = faRepairService.updateFaRepairInfo(FaRepair);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
			throw new UpdateException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}
	
	
	/**
	* @Author newway
	* @Description 删除固定资产大修改造信息接口
	* @Date 2019年8月12日下午5:41:57
	* @Param faRepairNo 大修改造序号
	* @Param fixedAssetNo 资产编码
	* @Param workNo 工单号
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "删除固定资产大修改造信息接口", notes = "删除固定资产大修改造信息接口", httpMethod = "DELETE")
	@DeleteMapping(value = "/deleteFaRepairInfo")
	public Result deleteFaRepairInfo(@RequestParam(value = "FaRepairNo", required = true) String FaRepairNo,
			@RequestParam(value = "fixedAssetNo", required = true) String fixedAssetNo,
			@RequestParam(value = "workNo", required = true) String workNo) {
		Result result = new Result();
		if(Common.isEmpty(FaRepairNo) || Common.isEmpty(fixedAssetNo)  || Common.isEmpty(workNo)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			Map<String, String> map = faRepairService.deleteFaRepairInfo(FaRepairNo,fixedAssetNo,workNo);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
			throw new DeleteException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}
	
	
	
}
